package com.lwg.vhr.service.impl;


import com.lwg.vhr.mapper.MenuMapper;
import com.lwg.vhr.mapper.MenuRoleMapper;
import com.lwg.vhr.model.Hr;
import com.lwg.vhr.model.Menu;
import com.lwg.vhr.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private MenuRoleMapper menuRoleMapper;

    @Override
    public List<Menu> getMenusByHrId() {

        //要传入id了，id从哪里来，我们登录的用户信息保存到security里面
        //SecurityContextHolder里面有一个getContext()方法.getAuthentication()它里面的getPrincipal()，
        // Principal它是当前登录的用户对象，然后强转成Hr对象再获取它里面的id
        return menuMapper.getMenusByHrId(((Hr) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId());
    }

    //@Cacheable
    @Override
    public List<Menu> getAllMenusByRole() {
        return menuMapper.getAllMenusByRole();
    }


    /**
     * @author: lwg
     * @Date: 2021/8/4 22:51
     * @Description: 权限组获得菜单树
     */
    @Override
    public List<Menu> getAllMenus() {
        return menuMapper.getAllMenus();
    }

    @Override
    public List<Integer> getMenusByRid(Integer rid) {
        return menuMapper.getMenusByRid(rid);
    }

    @Override
    public boolean updateMenuRole(Integer rid, Integer[] mids) {
        menuRoleMapper.deleteByRid(rid);
        if (mids == null || mids.length == 0) {
            return true;
        }
        Integer result = menuRoleMapper.insertRecord(rid, mids);
        return result == mids.length;
    }
}
